package com.mud.mudcommune.view.fragment.twoLevel;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.mud.mudcommune.R;
import com.mud.mudcommune.adapter.ArticleAdapter;
import com.mud.mudcommune.entry.ArticleEntry;
import com.mud.mudcommune.entry.ArticleResponse;
import com.mud.mudcommune.view.fragment.threeLevel.ArticleDetailsFragment;
import com.mud.mudcommune.utils.LogUtils;
import com.mud.mudcommune.http.ApiService;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class ArticleFragment extends Fragment implements Callback<ArticleResponse> {
    private static final String TAG = ArticleFragment.class.getSimpleName();
    private static final Fragment mArticleFragment = new ArticleFragment();
    @BindView(R.id.Article_ListView)
    ListView articleListView;
    @BindView(R.id.loading_layout)
    LinearLayout loadingLayout;
    private List<ArticleEntry> list;
    private ArticleAdapter adapter;

    public static Fragment getInstance() {
        return mArticleFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_article, container, false);
        ButterKnife.bind(this, view);
        requestData();
        final Fragment fragment = ArticleDetailsFragment.getInstance();
        articleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LogUtils.i(TAG, "fragment.isAdded(): " + fragment.isAdded() + ", article_id: " + list.get(position).getArticleId());
                if (!fragment.isAdded()) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("article_id", list.get(position).getArticleId());
                    fragment.setArguments(bundle);
                    getFragmentManager().beginTransaction()
                            .hide(mArticleFragment)
                            .add(R.id.main_frame, fragment)
                            .addToBackStack(null)
                            .commit();
                }
            }
        });

        return view;
    }

    @OnClick({R.id.back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                getFragmentManager().popBackStack();
                break;
        }
    }

    private void requestData() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(ApiService.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        Call<ArticleResponse> call = apiService.getArticle();
        call.enqueue(this);
    }

    @Override
    public void onResponse(Call<ArticleResponse> call, Response<ArticleResponse> response) {
        if (loadingLayout != null) {
            loadingLayout.setVisibility(View.GONE);
        }
        ArticleResponse body = response.body();
        if (body.getStatus() == 200) {
            list = body.getArticleList();
            adapter = new ArticleAdapter(getActivity(), list);
            articleListView.setAdapter(adapter);
        }
    }

    @Override
    public void onFailure(Call<ArticleResponse> call, Throwable t) {
        t.printStackTrace();
    }



}
